﻿using Furion.DatabaseAccessor;
using LimaLims.Core.Entity.Basic;
using LimaLims.Core.Enum;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LimaLims.Core.Entity.System
{
    [Comment("用户表")]
    public class SysUser : DEntityTenant, IEntityTypeBuilder<SysUser>, IEntitySeedData<SysUser>
    {
        /// <summary>
        /// 账号
        /// </summary>
        [Comment("账号")]
        [MaxLength(50)]
        public string Account { get; set; }

        /// <summary>
        /// 密码（默认MD5加密）
        /// </summary>
        [Comment("密码")]
        [MaxLength(50)]
        public string Password { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        [Comment("姓名")]
        [MaxLength(20)]
        public string Name { get; set; }

        /// <summary>
        /// 头像
        /// </summary>
        [Comment("头像")]
        public string Avatar { get; set; }

        /// <summary>
        /// 生日
        /// </summary>
        [Comment("生日")]
        public DateTime? Birthday { get; set; }

        /// <summary>
        /// 性别-男_1、女_2
        /// </summary>
        [Comment("性别-男_1、女_2")]
        public Gender Sex { get; set; }

        /// <summary>
        /// 邮箱
        /// </summary>
        [Comment("邮箱")]
        [MaxLength(50)]
        public string Email { get; set; }

        /// <summary>
        /// 手机
        /// </summary>
        [Comment("手机")]
        [MaxLength(20)]
        public string Phone { get; set; }

        /// <summary>
        /// 电话
        /// </summary>
        [Comment("电话")]
        [MaxLength(20)]
        public string Tel { get; set; }

        /// <summary>
        /// 最后登录IP
        /// </summary>
        [Comment("最后登录IP")]
        [MaxLength(20)]
        public string LastLoginIp { get; set; }

        /// <summary>
        /// 班组Id
        /// </summary>
        [Comment("班组Id")]
        public long? TeamId { get; set; }

        /// <summary>
        /// 班组
        /// </summary>
        public Team Team { get; set; }

        /// <summary>
        /// 最后登录时间
        /// </summary>
        [Comment("最后登录时间")]
        public DateTime? LastLoginTime { get; set; }

        /// <summary>
        /// 管理员类型
        /// </summary>
        [Comment("管理员类型-平台管理员_1、租户管理员_2、普通账号_3")]
        public AdminType AdminType { get; set; }

        /// <summary>
        /// 多对多（角色）
        /// </summary>
        public ICollection<SysRole> SysRoles { get; set; }

        /// <summary>
        /// 多对多中间表（用户-角色）
        /// </summary>
        public List<SysUserRole> SysUserRoles { get; set; }

        public void Configure(EntityTypeBuilder<SysUser> entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            entityBuilder.HasMany(p => p.SysRoles)
                .WithMany(p => p.SysUsers).UsingEntity<SysUserRole>(
                u => u.HasOne(c => c.SysRole).WithMany(c => c.SysUserRoles).HasForeignKey(c => c.SysRoleId),
                u => u.HasOne(c => c.SysUser).WithMany(c => c.SysUserRoles).HasForeignKey(c => c.SysUserId),
                u =>
                {
                    u.HasKey(c => new { c.SysUserId, c.SysRoleId });
                });
        }

        public IEnumerable<SysUser> HasData(DbContext dbContext, Type dbContextLocator)
        {
            return new[]
             {
                new SysUser
                {
              Id=142307070910551,TenantId=142307070918780,CreatedTime=DateTime.Now,Account="admin", Name="超级管理员",Password="e10adc3949ba59abbe56e057f20f883e",AdminType=AdminType.SuperAdmin,TeamId=242307070910559
                }

            };
        }
    }
}
